package io.gps.datatransfer.utils;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 字符操作工具类
 * @author bcs
 * @version 2.0, $Date:  2020-11-26 13:04:48 $
 */
public class ByteUtils {
    public static Random r = new Random(1);
    public static int getCheckCode(IoBuffer iob) {
        int pre = iob.get();
        while (iob.hasRemaining()) {
            pre ^= iob.get();
        }
        return pre & 0xff;
    }

    public static int check(IoBuffer iob, int len) {

        int pre = iob.get();
        for (int i = 1; i < len; i++) {
            pre ^= iob.get();
        }
        return pre & 0xff;
    }

    /**
     * 解析bcd码
     * @param bcd
     * @return String
     */
    public static String decodeBCD(int bcd){
        return decodeBCD(bcd, "");
    }
    /**
     * 解析bcd码
     * @param bcd
     * @param limit 分隔符
     * @return String
     */
    public static String decodeBCD(int bcd, String limit){

        return ((byte)((bcd& 0xf0)>>>4))+limit+((byte)(bcd& 0x0f));
    }

    public static String getTelphone(IoBuffer data){
        StringBuilder telphone = new StringBuilder();
        for(int i = 0;i<6;i++){
            telphone.append(decodeBCD(data.getUnsigned()));
        }
        return telphone.toString();
    }


    /**
     * 生成bcd编码
     * @param num 字符串长度必须2的整数倍
     * @return byte[]
     */
    public static byte[] encodeBCD(String num){

        return encodeBCD0(num.getBytes());
    }
    public static byte[] encodeBCD0(byte[] abt){
        int len = abt.length;

        byte bbt[] = new byte[len/2];

        int j, k;

        for (int p = 0; p < len/2; p++) {
            if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }

            if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            }else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

    static short seed;
    private static AtomicInteger count = new AtomicInteger(0);//线程安全的计数变量
    public static short getIncreaseNum(){
        if(count.get()>32700){
            count = new AtomicInteger(0);
        }
        seed= (short) count.incrementAndGet();
        return seed;
    }

    public static String formatBinary(String msg,int format)     {
        if(msg.length() >= format)
            return msg;
        return String.format("%0"+(format-msg.length())+"d%s",0,msg);
    }

    public static String getSouDataLog(IoBuffer iob) {
        StringBuilder outstr=new StringBuilder();
        byte[] sss=iob.array();
        for (int i = 0; i < sss.length; i++) {
            String outss=("000"+Integer.toHexString((sss[i]& 0xFF)).toUpperCase());
            outstr.append(outss.substring(outss.length()-2));
        }
        return outstr.toString();

    }

    public static int oldcheck(IoBuffer iob) {

        int pre = iob.get();

        while (iob.hasRemaining()) {
            pre ^= iob.get();
        }

        return pre & 0xff;
    }


    public static byte[] getLenByte(int len) {
        byte[] tempbyte=new byte[len];
        for (int i = 0; i <len ; i++) {
            tempbyte[i]=0x0;
        }
        return tempbyte;
    }

    /**
     * 骐达其他数据
     * @param newdata
     * @param olddate
     * @param obj
     */
    public static void extAbandon(IoBuffer newdata,IoBuffer olddate,byte[] obj){
        olddate.get(obj);
        newdata.put(obj);
    }



    public static void main(String[] args)
    {    

      for(int i=0 ; i<1000 ;  i++)
      {
        int ran1 = r.nextInt(5);
        System.out.println(ran1+"------");
      }
    }

    public static IoBuffer getMessage(IoBuffer data) throws Exception{
        IoBuffer buf = IoBuffer.allocate(15);
        buf.setAutoExpand(true);
        byte b;
        while(data.hasRemaining()){
            b = data.get();
            if(b==0x7d && data.hasRemaining()){
                b = data.get();
                if(b==0x02){
                    buf.put((byte)0x7e);
                }else if(b==0x01){
                    buf.put((byte)0x7d);
                }else{
                    throw new Exception("非法信息");
                }
            }else{
                buf.put(b);
            }
        }
        buf.shrink();  //释放无用资源
        buf.flip();
        return buf;
    }

    public static void getMessage(IoBuffer srcdata,IoBuffer buf) throws Exception{
        byte b;
        while(srcdata.hasRemaining()){
            b = srcdata.get();
            if(b==0x7d && srcdata.hasRemaining()){
                b = srcdata.get();
                if(b==0x02){
                    buf.put((byte)0x7e);
                }else if(b==0x01){
                    buf.put((byte)0x7d);
                }else{
                    throw new Exception("非法信息");
                }
            }else{
                buf.put(b);
            }
        }
        buf.shrink();  //释放无用资源
        buf.flip();
    }
}
